<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\LoginForm;
use backend\models\Admin;

/**
 * Site controller
 */
class SiteController extends SuperController {

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                        [
                        'actions' => [ 'login', 'error', 'captcha', 'seekpassword', 'setpassword' ],
                        'allow'   => true,
                    ],
                        [
                        'actions' => [ 'logout', 'index', ],
                        'allow'   => true,
                        'roles'   => [ '@' ],
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'logout' => [ 'post' ],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error'   => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class'     => 'yii\captcha\CaptchaAction',
                'maxLength' => 4,
                'minLength' => 4
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        else {
            return $this->render('login', [
                        'model' => $model,
            ]);
        }
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionSeekpassword()
    {
        $model = new Admin;
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->seekPass($post)) {
                return $this->success('login');
            }
            else {
                return $this->error('用户名与邮箱不匹配');
            }
        }
        $this->layout = "main-login";
        return $this->render('seekpassword', [ 'model' => $model ]);
    }

    public function actionSetpassword()
    {
        $this->layout = false;
        $time = Yii::$app->request->get("timestamp");
        $username = Yii::$app->request->get("username");
        $token = Yii::$app->request->get("token");
        $model = new Admin();
        $myToken = $model->createToken($username, $time);
        if ($token != $myToken) {
            return $this->error("验证失败", [ 'seekpassword' ]);
//            Yii::$app->end();
        }
        if (time() - $time > 300) {
            return $this->error("已经过了限定的时间", [ 'seekpassword' ]);
//            $this->redirect([ 'login' ]);
//            Yii::$app->end();
        }
        $model = Admin::findOne([ 'username' => $username ]);
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            $password = $post['Admin']['password_hash'];
            $model->cryptPassword($password);
            if ($model->save()) {
                return $this->success([ 'login' ]);
            }
        }
        $model->password_hash = "";
        $this->layout = "main-login";
        return $this->render('setpassword', [ 'model' => $model ]);
    }

}
